<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>OpenMesh: Deleting geometry elements</title>
<link href="tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="dynsections.js"></script>
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtreedata.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
</script>
<link href="search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="search/searchdata.js"></script>
<script type="text/javascript" src="search/search.js"></script>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="logo_align.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="rwth_vci_rgb.jpg"/></td>
  <td id="projectalign" style="padding-left: 0.5em;">
   <div id="projectname">OpenMesh
   </div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "search",false,'Search');
</script>
<script type="text/javascript" src="menudata.js"></script>
<script type="text/javascript" src="menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
      <div id="nav-sync" class="sync"></div>
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
$(document).ready(function(){initNavTree('a04343.html','');});
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">Deleting geometry elements </div>  </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><p>This small example shows how to remove faces and vertices from a mesh.</p>
<p>We basically use the geometry created in <a class="el" href="a04336.html">First Steps - Building a cube</a>.</p>
<p>If we want our mesh class to be able to remove vertices, faces or edges we have to extend the default traits for our mesh class. Vertices, faces and (half-)edges need the <a class="el" href="a01238.html#ab78a93560926cd2f9958cb028f7ea96dad3e94e10c76894ebce6048b8bbb77a74" title="Add status to mesh item (all items) ">OpenMesh::Attributes::Status</a> attribute which is used to hold the flag "deleted" if an element is deleted.</p>
<p>Instead of defining the required attributes via traits on compile-time, it is possible to request attributes dynamically on run-time by requesting them. In this example, we want to delete faces, edges and vertices, therefore requesting the status attribute is required. </p><dl class="section note"><dt>Note</dt><dd>You have to request attributes before you use them, if you don't enable them via traits.</dd></dl>
 <div class="fragment"><div class="line">  <span class="comment">// the request has to be called before a vertex/face/edge can be deleted. it grants access to the status attribute</span></div><div class="line">  mesh.request_face_status();</div><div class="line">  mesh.request_edge_status();</div><div class="line">  mesh.request_vertex_status();</div></div><!-- fragment --></p>
<p>After having created the geometry of the cube one can delete faces and vertices by simply calling delete_vertices() (delete_faces() or delete_edges() respectively).</p>
<p>Note that there is actually no way to directly delete halfedges since they are automatically affected when the parent edge is marked as deleted!</p>
<p>The status whether an element is marked as deleted can be requested by</p>
<div class="fragment"><div class="line">mesh.status(handle).deleted();  <span class="comment">// true if element handle is marked as deleted</span></div><div class="line">                                <span class="comment">// false otherwise</span></div></div><!-- fragment --><p>where handle is either a vertex-, edge- or face-handle.</p>
<p>In this example we delete all faces except one and the corresponding vertices. The code looks like this</p>
 <div class="fragment"><div class="line">  <span class="comment">// Delete face 0</span></div><div class="line">  mesh.delete_face(fhandle[0], <span class="keyword">false</span>);</div><div class="line">  <span class="comment">// ... face 2</span></div><div class="line">  mesh.delete_face(fhandle[2], <span class="keyword">false</span>);</div><div class="line">  <span class="comment">// ... face 3</span></div><div class="line">  mesh.delete_face(fhandle[3], <span class="keyword">false</span>);</div><div class="line">  <span class="comment">// ... face 4</span></div><div class="line">  mesh.delete_face(fhandle[4], <span class="keyword">false</span>);</div><div class="line">  <span class="comment">// ... face 5</span></div><div class="line">  mesh.delete_face(fhandle[5], <span class="keyword">false</span>);</div><div class="line">  </div><div class="line"></div><div class="line">  <span class="comment">// If isolated vertices result in a face deletion</span></div><div class="line">  <span class="comment">// they have to be deleted manually. If you want this</span></div><div class="line">  <span class="comment">// to happen automatically, change the second parameter</span></div><div class="line">  <span class="comment">// to true.</span></div><div class="line"></div><div class="line">  <span class="comment">// Now delete the isolated vertices 0, 1, 2 and 3</span></div><div class="line">  mesh.delete_vertex(vhandle[0], <span class="keyword">false</span>);</div><div class="line">  mesh.delete_vertex(vhandle[1], <span class="keyword">false</span>);</div><div class="line">  mesh.delete_vertex(vhandle[2], <span class="keyword">false</span>);</div><div class="line">  mesh.delete_vertex(vhandle[3], <span class="keyword">false</span>);</div></div><!-- fragment --></p>
<p>Now the deleted faces and vertices are marked as "deleted" internally. Call garbage_collection() to definitely remove them from memory.</p>
 <div class="fragment"><div class="line">  <span class="comment">// Delete all elements that are marked as deleted</span></div><div class="line">  <span class="comment">// from memory.</span></div><div class="line">  mesh.garbage_collection();</div></div><!-- fragment --></p>
<p>The full source code of the example:</p>
<div class="fragment"><div class="line"><span class="comment">/* ========================================================================= *</span></div><div class="line"><span class="comment"> *                                                                           *</span></div><div class="line"><span class="comment"> *                               OpenMesh                                    *</span></div><div class="line"><span class="comment"> *           Copyright (c) 2001-2015, RWTH-Aachen University                 *</span></div><div class="line"><span class="comment"> *           Department of Computer Graphics and Multimedia                  *</span></div><div class="line"><span class="comment"> *                          All rights reserved.                             *</span></div><div class="line"><span class="comment"> *                            www.openmesh.org                               *</span></div><div class="line"><span class="comment"> *                                                                           *</span></div><div class="line"><span class="comment"> *---------------------------------------------------------------------------*</span></div><div class="line"><span class="comment"> * This file is part of OpenMesh.                                            *</span></div><div class="line"><span class="comment"> *---------------------------------------------------------------------------*</span></div><div class="line"><span class="comment"> *                                                                           *</span></div><div class="line"><span class="comment"> * Redistribution and use in source and binary forms, with or without        *</span></div><div class="line"><span class="comment"> * modification, are permitted provided that the following conditions        *</span></div><div class="line"><span class="comment"> * are met:                                                                  *</span></div><div class="line"><span class="comment"> *                                                                           *</span></div><div class="line"><span class="comment"> * 1. Redistributions of source code must retain the above copyright notice, *</span></div><div class="line"><span class="comment"> *    this list of conditions and the following disclaimer.                  *</span></div><div class="line"><span class="comment"> *                                                                           *</span></div><div class="line"><span class="comment"> * 2. Redistributions in binary form must reproduce the above copyright      *</span></div><div class="line"><span class="comment"> *    notice, this list of conditions and the following disclaimer in the    *</span></div><div class="line"><span class="comment"> *    documentation and/or other materials provided with the distribution.   *</span></div><div class="line"><span class="comment"> *                                                                           *</span></div><div class="line"><span class="comment"> * 3. Neither the name of the copyright holder nor the names of its          *</span></div><div class="line"><span class="comment"> *    contributors may be used to endorse or promote products derived from   *</span></div><div class="line"><span class="comment"> *    this software without specific prior written permission.               *</span></div><div class="line"><span class="comment"> *                                                                           *</span></div><div class="line"><span class="comment"> * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS       *</span></div><div class="line"><span class="comment"> * &quot;AS IS&quot; AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED *</span></div><div class="line"><span class="comment"> * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A           *</span></div><div class="line"><span class="comment"> * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER *</span></div><div class="line"><span class="comment"> * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,  *</span></div><div class="line"><span class="comment"> * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,       *</span></div><div class="line"><span class="comment"> * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR        *</span></div><div class="line"><span class="comment"> * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF    *</span></div><div class="line"><span class="comment"> * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING      *</span></div><div class="line"><span class="comment"> * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS        *</span></div><div class="line"><span class="comment"> * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.              *</span></div><div class="line"><span class="comment"> *                                                                           *</span></div><div class="line"><span class="comment"> * ========================================================================= */</span></div><div class="line"></div><div class="line"><span class="preprocessor">#include &lt;iostream&gt;</span></div><div class="line"><span class="comment">// -------------------- OpenMesh</span></div><div class="line"><span class="preprocessor">#include &lt;OpenMesh/Core/IO/MeshIO.hh&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;OpenMesh/Core/Mesh/PolyMesh_ArrayKernelT.hh&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;OpenMesh/Core/System/config.h&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;OpenMesh/Core/Mesh/Status.hh&gt;</span></div><div class="line"></div><div class="line"><span class="comment">// ----------------------------------------------------------------------------</span></div><div class="line"></div><div class="line"><span class="keyword">struct </span><a class="code" href="a01881.html">MyTraits</a> : <span class="keyword">public</span> <a class="code" href="a02469.html">OpenMesh::DefaultTraits</a></div><div class="line">{</div><div class="line">};</div><div class="line"></div><div class="line"></div><div class="line"><span class="keyword">typedef</span> <a class="code" href="a02385.html">OpenMesh::PolyMesh_ArrayKernelT&lt;MyTraits&gt;</a>  MyMesh;</div><div class="line"></div><div class="line"></div><div class="line"><span class="comment">// ----------------------------------------------------------------------------</span></div><div class="line"><span class="comment">// Build a simple cube and delete it except one face</span></div><div class="line">  </div><div class="line"><span class="keywordtype">int</span> main()</div><div class="line">{</div><div class="line">  MyMesh mesh;</div><div class="line"></div><div class="line">  <span class="comment">// the request has to be called before a vertex/face/edge can be deleted. it grants access to the status attribute</span></div><div class="line">  mesh.request_face_status();</div><div class="line">  mesh.request_edge_status();</div><div class="line">  mesh.request_vertex_status();</div><div class="line"></div><div class="line">  <span class="comment">// generate vertices</span></div><div class="line"></div><div class="line">  <a class="code" href="a02389.html#a0fc18e13d70825938274e3faf7b55e01">MyMesh::VertexHandle</a> vhandle[8];</div><div class="line">  MyMesh::FaceHandle   fhandle[6];</div><div class="line"></div><div class="line">  vhandle[0] = mesh.<a class="code" href="a02389.html#a5338335bff53d9b88afc14634c456908">add_vertex</a>(<a class="code" href="a02389.html#aed10fb4dfb536b53a5e20c8f2442eadf">MyMesh::Point</a>(-1, -1,  1));</div><div class="line">  vhandle[1] = mesh.<a class="code" href="a02389.html#a5338335bff53d9b88afc14634c456908">add_vertex</a>(<a class="code" href="a02389.html#aed10fb4dfb536b53a5e20c8f2442eadf">MyMesh::Point</a>( 1, -1,  1));</div><div class="line">  vhandle[2] = mesh.<a class="code" href="a02389.html#a5338335bff53d9b88afc14634c456908">add_vertex</a>(<a class="code" href="a02389.html#aed10fb4dfb536b53a5e20c8f2442eadf">MyMesh::Point</a>( 1,  1,  1));</div><div class="line">  vhandle[3] = mesh.<a class="code" href="a02389.html#a5338335bff53d9b88afc14634c456908">add_vertex</a>(<a class="code" href="a02389.html#aed10fb4dfb536b53a5e20c8f2442eadf">MyMesh::Point</a>(-1,  1,  1));</div><div class="line">  vhandle[4] = mesh.<a class="code" href="a02389.html#a5338335bff53d9b88afc14634c456908">add_vertex</a>(<a class="code" href="a02389.html#aed10fb4dfb536b53a5e20c8f2442eadf">MyMesh::Point</a>(-1, -1, -1));</div><div class="line">  vhandle[5] = mesh.<a class="code" href="a02389.html#a5338335bff53d9b88afc14634c456908">add_vertex</a>(<a class="code" href="a02389.html#aed10fb4dfb536b53a5e20c8f2442eadf">MyMesh::Point</a>( 1, -1, -1));</div><div class="line">  vhandle[6] = mesh.<a class="code" href="a02389.html#a5338335bff53d9b88afc14634c456908">add_vertex</a>(<a class="code" href="a02389.html#aed10fb4dfb536b53a5e20c8f2442eadf">MyMesh::Point</a>( 1,  1, -1));</div><div class="line">  vhandle[7] = mesh.<a class="code" href="a02389.html#a5338335bff53d9b88afc14634c456908">add_vertex</a>(<a class="code" href="a02389.html#aed10fb4dfb536b53a5e20c8f2442eadf">MyMesh::Point</a>(-1,  1, -1));</div><div class="line"></div><div class="line"></div><div class="line">  <span class="comment">// generate (quadrilateral) faces</span></div><div class="line"></div><div class="line">  std::vector&lt;MyMesh::VertexHandle&gt;  tmp_face_vhandles;</div><div class="line"></div><div class="line">  tmp_face_vhandles.clear();</div><div class="line">  tmp_face_vhandles.push_back(vhandle[0]);</div><div class="line">  tmp_face_vhandles.push_back(vhandle[1]);</div><div class="line">  tmp_face_vhandles.push_back(vhandle[2]);</div><div class="line">  tmp_face_vhandles.push_back(vhandle[3]);</div><div class="line">  fhandle[0] = mesh.add_face(tmp_face_vhandles);</div><div class="line"> </div><div class="line">  tmp_face_vhandles.clear();</div><div class="line">  tmp_face_vhandles.push_back(vhandle[7]);</div><div class="line">  tmp_face_vhandles.push_back(vhandle[6]);</div><div class="line">  tmp_face_vhandles.push_back(vhandle[5]);</div><div class="line">  tmp_face_vhandles.push_back(vhandle[4]);</div><div class="line">  fhandle[1] = mesh.add_face(tmp_face_vhandles);</div><div class="line"> </div><div class="line">  tmp_face_vhandles.clear();</div><div class="line">  tmp_face_vhandles.push_back(vhandle[1]);</div><div class="line">  tmp_face_vhandles.push_back(vhandle[0]);</div><div class="line">  tmp_face_vhandles.push_back(vhandle[4]);</div><div class="line">  tmp_face_vhandles.push_back(vhandle[5]);</div><div class="line">  fhandle[2] = mesh.add_face(tmp_face_vhandles);</div><div class="line"> </div><div class="line"></div><div class="line">  tmp_face_vhandles.clear();</div><div class="line">  tmp_face_vhandles.push_back(vhandle[2]);</div><div class="line">  tmp_face_vhandles.push_back(vhandle[1]);</div><div class="line">  tmp_face_vhandles.push_back(vhandle[5]);</div><div class="line">  tmp_face_vhandles.push_back(vhandle[6]);</div><div class="line">  fhandle[3] = mesh.add_face(tmp_face_vhandles);</div><div class="line"></div><div class="line"></div><div class="line">  tmp_face_vhandles.clear();</div><div class="line">  tmp_face_vhandles.push_back(vhandle[3]);</div><div class="line">  tmp_face_vhandles.push_back(vhandle[2]);</div><div class="line">  tmp_face_vhandles.push_back(vhandle[6]);</div><div class="line">  tmp_face_vhandles.push_back(vhandle[7]);</div><div class="line">  fhandle[4] = mesh.add_face(tmp_face_vhandles);</div><div class="line"> </div><div class="line"></div><div class="line">  tmp_face_vhandles.clear();</div><div class="line">  tmp_face_vhandles.push_back(vhandle[0]);</div><div class="line">  tmp_face_vhandles.push_back(vhandle[3]);</div><div class="line">  tmp_face_vhandles.push_back(vhandle[7]);</div><div class="line">  tmp_face_vhandles.push_back(vhandle[4]);</div><div class="line">  fhandle[5] = mesh.add_face(tmp_face_vhandles);</div><div class="line"></div><div class="line">  <span class="comment">// And now delete all faces and vertices</span></div><div class="line">  <span class="comment">// except face (vh[7], vh[6], vh[5], vh[4])</span></div><div class="line">  <span class="comment">// whose handle resides in fhandle[1]</span></div><div class="line"></div><div class="line">   </div><div class="line">  <span class="comment">// Delete face 0</span></div><div class="line">  mesh.delete_face(fhandle[0], <span class="keyword">false</span>);</div><div class="line">  <span class="comment">// ... face 2</span></div><div class="line">  mesh.delete_face(fhandle[2], <span class="keyword">false</span>);</div><div class="line">  <span class="comment">// ... face 3</span></div><div class="line">  mesh.delete_face(fhandle[3], <span class="keyword">false</span>);</div><div class="line">  <span class="comment">// ... face 4</span></div><div class="line">  mesh.delete_face(fhandle[4], <span class="keyword">false</span>);</div><div class="line">  <span class="comment">// ... face 5</span></div><div class="line">  mesh.delete_face(fhandle[5], <span class="keyword">false</span>);</div><div class="line">  </div><div class="line"></div><div class="line">  <span class="comment">// If isolated vertices result in a face deletion</span></div><div class="line">  <span class="comment">// they have to be deleted manually. If you want this</span></div><div class="line">  <span class="comment">// to happen automatically, change the second parameter</span></div><div class="line">  <span class="comment">// to true.</span></div><div class="line"></div><div class="line">  <span class="comment">// Now delete the isolated vertices 0, 1, 2 and 3</span></div><div class="line">  mesh.delete_vertex(vhandle[0], <span class="keyword">false</span>);</div><div class="line">  mesh.delete_vertex(vhandle[1], <span class="keyword">false</span>);</div><div class="line">  mesh.delete_vertex(vhandle[2], <span class="keyword">false</span>);</div><div class="line">  mesh.delete_vertex(vhandle[3], <span class="keyword">false</span>);</div><div class="line"></div><div class="line">  <span class="comment">// Delete all elements that are marked as deleted</span></div><div class="line">  <span class="comment">// from memory.</span></div><div class="line">  mesh.garbage_collection();</div><div class="line"></div><div class="line">  <span class="comment">// write mesh to output.obj</span></div><div class="line">  <span class="keywordflow">try</span> {</div><div class="line">        <span class="keywordflow">if</span> ( !<a class="code" href="a01236.html#a18286a792ebf896872f5e26182fc5c7b">OpenMesh::IO::write_mesh</a>(mesh, <span class="stringliteral">&quot;output.off&quot;</span>) ) {</div><div class="line">          std::cerr &lt;&lt; <span class="stringliteral">&quot;Cannot write mesh to file &#39;output.off&#39;&quot;</span> &lt;&lt; std::endl;</div><div class="line">          <span class="keywordflow">return</span> 1;</div><div class="line">        }</div><div class="line">  }</div><div class="line">  <span class="keywordflow">catch</span>( std::exception&amp; x )</div><div class="line">  {</div><div class="line">    std::cerr &lt;&lt; x.what() &lt;&lt; std::endl;</div><div class="line">    <span class="keywordflow">return</span> 1;</div><div class="line">  }</div><div class="line"> </div><div class="line">  <span class="keywordflow">return</span> 0;</div><div class="line">}</div></div><!-- fragment --> </div></div><!-- contents -->
</div><!-- doc-content -->
<hr>
<address>
<small>
<a href="http://www.rwth-graphics.de" style="text-decoration:none;">
</a>
Project <b>OpenMesh</b>, 
&copy;&nbsp; Computer Graphics Group, RWTH Aachen.
Documentation generated using 
<a class="el" href="http://www.doxygen.org/index.html">
<b>doxygen</b>
</a>.
</small>
</address>
</body>
</html>
